const host = 'http://localhost:8000'
function save() {
  const id = new URLSearchParams(location.search).get('id')
  const bookname = document.querySelector('.bookname').value
  const author = document.querySelector('.author').value
  const descriptions = document.querySelector('.descriptions').value
  if (id) {
    axios.put(`${host}/book/${id}`, { bookname, author, descriptions }).then(res => {
      if (res.data.code = 200) {
        alert('修改成功')
        location.href = './book.html'
      }
    })
  } else {
    axios.post(`${host}/book/`, { bookname, author, descriptions }).then(res => {
      if (res.data.code = 200) {
        alert('添加成功')
        location.href = './book.html'
      }
    })
  }
}

function back() {
  location.href = './book.html'
}

function init() {
  const id = new URLSearchParams(location.search).get('id')
  if (id) {
    axios.get(`${host}/book/${id}`).then(res => {
      const data = res.data.data
      document.querySelector('.bookname').value = data.bookname
      document.querySelector('.author').value = data.author
      document.querySelector('.descriptions').value = data.descriptions
    })
  }
}
init()